package org.zjx.dao.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.zjx.dao.entity.DailyTasks;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 
 * @since 2025-05-28
 */
public interface DailyTasksMapper extends BaseMapper<DailyTasks> {
    @Select("SELECT dt.* FROM daily_tasks dt " +
            "INNER JOIN role r ON dt.role_id = r.id " +
            "INNER JOIN account a ON r.account_id = a.id " +
            "WHERE dt.status = 2 " +  // 主查询条件
            "AND NOT EXISTS ( " +  // 子查询排除有执行中任务的账号
            "    SELECT 1 FROM device_daily_task ddt " +
            "    INNER JOIN daily_tasks sub_dt ON ddt.daily_tasks_id = sub_dt.id " +
            "    INNER JOIN role sub_r ON sub_r.id = sub_dt.role_id " +
            "    INNER JOIN account sub_a ON sub_r.account_id = sub_a.id " +
            "    WHERE sub_a.id = a.id " +  // 关联当前账号
            "    AND sub_dt.status = 3 " +  // 存在执行中任务
            ") " +
            "${ew.customSqlSegment}")  // 保留动态条件扩展
    List<DailyTasks> queryTasksOnAccountUnUsd(@Param("ew") QueryWrapper<DailyTasks> wrapper);


}
